/**
 * Created by CSJ11 on 2017/9/12.
 */
'use strict';

angular.module('app')
    .controller('afficheController', ['$rootScope', '$scope', '$http', '$state', 'toaster', '$timeout',
        function ($rootScope, $scope, $http, $state, toaster, $timeout) {
            $scope.title = '资讯管理';
            $scope.param = {};
            $scope.loading = false;
            console.log($scope.param);
            $scope.search = function () {
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/affiche/getAffiches',
                    data: $scope.param
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.param.pageNum = 1;
                $.ajax({
                    url: $scope.app.project_name + '/affiche/getAffiches',
                    data: $scope.param
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.makeList = function () {
                $.ajax({
                    url: $scope.app.project_name + '/affiche/getType',
                }).then(function (result) {
                    $scope.$apply(function () {
                        $scope.typeList = result.data;
                        $scope.results = [];
                        for (var i = 0; i < $scope.typeList.length; i++) {
                            var obj = {};
                            obj.typeId = $scope.typeList[i].typeId;
                            obj.typeName = $scope.typeList[i].typeName;
                            obj.sortNum = $scope.typeList[i].sortNum;
                            obj.isEdit = '1';
                            obj.saveType = 'update';
                            $scope.results.push(obj);
                        }
                    })
                });
            };

            $scope.makeLabList = function () {
                $.ajax({
                    url: $scope.app.project_name + '/affiche/getLabs',
                    data: {pageNum: $scope.labpageNum}
                }).then(function (result) {
                    $scope.$apply(function () {
                        $scope.labList = result.data.records;
                        $scope.labpageInfo = result.data;
                        $scope.labresults = [];
                        for (var i = 0; i < $scope.labList.length; i++) {
                            var obj = {};
                            obj.labId = $scope.labList[i].labId;
                            obj.labName = $scope.labList[i].labName;
                            obj.labType = $scope.labList[i].labType;
                            obj.affsum = $scope.labList[i].affsum;
                            obj.isEdit = '1';
                            obj.saveType = 'update';
                            $scope.labresults.push(obj);
                        }
                    })
                });
            };


            $scope.search();
            $scope.makeList();
            $scope.clearSearch = function () {
                $scope.param.title = null;
                $scope.param.type = null;
                $scope.searchRe();
            }


            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.param.pageNum = pageNo;
                $scope.search();
            };
            $scope.labpagination = function (pageNo) {
                $scope.labpageNum = pageNo;
                $scope.makeLabList();
            }

            $scope.showTypeModel = function () {
                $scope.makeList();
                $('#TypeModel').modal('show', true);
            }

            $scope.showLabModel = function () {
                $scope.makeLabList();
                $scope.labpageNum = 1;
                $('#LabModel').modal('show', true);
            }

            $scope.addTr = function () {
                if($scope.results.length==0){
                    var obj = {};
                    obj.typeId = '';
                    obj.typeName = '';
                    obj.sortNum = '';
                    obj.isEdit = '0';
                    obj.saveType = 'save';
                    $scope.results.push(obj);
                    return;
                }else if($scope.results[$scope.results.length - 1].saveType == 'save') {
                    return;
                }else {
                    var obj = {};
                    obj.typeId = '';
                    obj.typeName = '';
                    obj.sortNum = '';
                    obj.isEdit = '0';
                    obj.saveType = 'save';
                    $scope.results.push(obj);
                }

            }

            $scope.addLabTr = function () {
                if($scope.labresults.length==0){
                    var obj = {};
                    obj.labId = '';
                    obj.labName = '';
                    obj.labType = '';
                    obj.affsum = '';
                    obj.isEdit = '0';
                    obj.saveType = 'save';
                    $scope.labresults.push(obj);
                }
                if ($scope.labresults[$scope.labresults.length - 1].saveType == 'save') {
                    return;
                }
                var obj = {};
                obj.labId = '';
                obj.labName = '';
                obj.labType = '';
                obj.affsum = '';
                obj.isEdit = '0';
                obj.saveType = 'save';
                $scope.labresults.push(obj);
            }
            $scope.edit = function (item, $event) {
                var checkbox = $event.target;
                if (item.isEdit == '1') {
                    item.isEdit = '0';
                    return;
                } else {
                    var flag = true;
                    $(checkbox).parent().siblings().find(".require").each(function () {
                        if ($(this).val() == "" || $(this).val() == null) {
                            $(this).tooltip({"trigger": 'manual', 'title': $(this).data('title') + "不能为空"});
                            $(this).tooltip('show');
                            $(this).hover(function () {
                                $(this).tooltip('hide');
                            });
                            flag = false;
                            return false;
                        }
                    })
                }
                checkbox.disabled = true;
                $.ajax({
                    url: $scope.app.project_name + '/affiche/saveType',
                    data: item
                }).then(function (result) {
                    if (result.data > 0) {
                        item.isEdit = '1';
                        $scope.makeList();
                        $scope.$apply();
                    } else {
                        $scope.makeList();
                        $scope.$apply();
                    }
                });
                checkbox.disabled = false;
            }

            $scope.editlab = function (item, $event) {
                var checkbox = $event.target;
                if (item.isEdit == '1') {
                    item.isEdit = '0';
                    return;
                } else {
                    var flag = true;
                    $(checkbox).parent().siblings().find(".require").each(function () {
                        if ($(this).val() == "" || $(this).val() == null) {
                            $(this).tooltip({"trigger": 'manual', 'title': $(this).data('title') + "不能为空"});
                            $(this).tooltip('show');
                            $(this).hover(function () {
                                $(this).tooltip('hide');
                            });
                            flag = false;
                            return false;
                        }
                    })
                }
                checkbox.disabled = true;
                $.ajax({
                    url: $scope.app.project_name + '/affiche/saveLab',
                    data: item
                }).then(function (result) {
                    if (result.data > 0) {
                        item.isEdit = '1';
                        $scope.makeLabList();
                        $scope.$apply();
                    } else {
                        $scope.makeLabList();
                        $scope.$apply();
                    }
                });
                checkbox.disabled = false;
            }

            $scope.disableItem = function (item, $event) {
                var checkbox = $event.target;
                if (item.saveType == 'save') {
                    $scope.results.splice($scope.results.length - 1, 1);
                    $(checkbox).parent().parent().remove();
                } else {
                    checkbox.disabled = true;
                    $.ajax({
                        url: $scope.app.project_name + '/affiche/deleteType',
                        data: item
                    }).then(function (result) {
                        if (result.httpCode == 409) {
                            alert("该类型下还有" + result.data + "条资讯");
                        } else {
                            $scope.makeList();
                            $scope.$apply();
                        }
                    });
                    checkbox.disabled = false;
                }
            }
            $scope.disableLabItem = function (item, $event) {
                var checkbox = $event.target;
                if (item.saveType == 'save') {
                    $scope.labresults.splice($scope.labresults.length - 1, 1);
                    $(checkbox).parent().parent().remove();
                } else {
                    checkbox.disabled = true;
                    $.ajax({
                        url: $scope.app.project_name + '/affiche/deleteLab',
                        data: item
                    }).then(function (result) {
                        if (result.httpCode == 409) {
                            alert("该类型下还有" + result.data + "条资讯");
                        } else {
                            $scope.makeLabList();
                            $scope.$apply();
                        }
                    });
                    checkbox.disabled = false;
                }
            }


            // 拖拽排序
            $scope.editSort = function ($event) {
                if ($scope.param.type == '' || $scope.param.type == null) {
                    $scope.alertModal({'content': '请选择类型!'});
                    return;
                }
                $scope.search();
                $("#bannerList").sortable({
                    opacity: 0.5,//拖动的透明度
                    revert: true, //缓冲效果
                    cursor: 'move', //拖动的时候鼠标样式
                    connectWith: ".column", scrollSpeed: 40
                });
                $("#bannerList").disableSelection();

                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    $(".editSortBtn").hide();
                    $(".saveSortBtn").show();
                }
            }

            $scope.saveSort = function ($event) {
                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                var sortArray = new Array();//声明一个数组来保存多个序号的值
                for (var j = 0; j < $scope.pageInfo.records.length; j++) {
                    var tr = $(".sort  tbody tr")[j];
                    var id = $($(tr).find("td")[1]).html();
                    var sort = j + 1;
                    sortArray[j] = sort;//用来存放序号
                    var batchUpdate = id + "-" + sort;
                    // alert(batchUpdate);
                    batchUpdates[j] = batchUpdate;
                }
                $.ajax({
                    url: $scope.app.project_name + '/affiche/editSort',
                    data: {'ids': batchUpdates},
                    traditional: true
                }).then(function (result) {
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content": "操作成功"});
                        $(".editSortBtn").show();
                        $("#sortId").show();
                        $(".saveSortBtn").hide();
                        $("#bannerList").sortable('destroy');//解除拖拽
                        $timeout(function () {
                            $scope.search();
                        }, 1000);
                    } else {
                        $scope.msg = result.msg;
                    }
                });
            }

            $scope.selected = [];
            var updateSelected = function (action, contact) {
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1) {
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }

                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1) {
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };

            $scope.updateSelection = function ($event, contact) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1) {
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }
                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1) {
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };
            // 全选操作
            $scope.selectAll = function ($event) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    var contact = $scope.pageInfo.records[i];
                    updateSelected(action, contact);
                }
            };

            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };

            $scope.batchDelete = function () {
                $scope.alertMessage({
                    text: "确定做此操作吗？", //内容
                    btnCancel: true, //取消按钮
                    btnConfirm: true //确认按钮
                }).then(function () {
                    $.ajax({
                        url: $scope.app.project_name + '/affiche/delete',
                        data: {'ids': $scope.selected}
                    }).then(function (result) {
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                            $scope.selected = [];
                            $scope.search();
                            $scope.alertModal({"content": "操作成功"});
                        } else {
                            $scope.alertModal({"content": "操作失败"});
                        }
                    });

                })
            }
        }]);